<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Info extends PNC_Controller {

	public function __Construct() {
		parent::__Construct();
		$this->check_access();
		$this->load->model(array("mod_gm", "mod_record", "mod_option"));
	}

	/*
	 * Index
	 * @access public
	 * @return void
	 */

	public function index() {
		redirect('info/show_info/');
	}

	public function show_info() {
		if ($this->check_session()) {
			$this->session->set_userdata("cur_page", $this->uri->segment(3));
			$this->session->set_userdata("pagination", $this->uri->segment(4));
			$data['title'] = 'Personal leaving Request';
			$config['total_rows'] = PNC_Model::personal_leaving($this->session->userdata('staff_id'), 'pending');
			$this->session->set_userdata('num_own_pending', $config['total_rows']);
			$num_per_page = 10;
			/* in case user delete query string */
			if ($this->session->userdata('per_page')) {
				$this->session->unset_userdata('per_page');
			}
			/* get limit per page from query string */
			if ($this->input->get('limit', TRUE)) {
				$this->session->set_userdata('per_page', $this->input->get('limit', TRUE));
				$num_per_page = $this->session->userdata('per_page');
			}
			$config['total_rows'] = ''; // initialize total records
			$config['base_url'] = site_url('info/show_info/own_pending'); // initialize base url
			if ($this->uri->segment(3) === 'own_pending' OR !$this->uri->segment(3)) {
				$config['base_url'] = site_url('info/show_info/own_pending');
				$config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 0);
			} else if ($this->uri->segment(3) === 'own_approved') {
				$config['base_url'] = site_url('info/show_info/own_approved');
				$config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 1);
			}
			$config['per_page'] = $num_per_page;
			$config['uri_segment'] = 4;
			$choice = $config["total_rows"] / $config["per_page"];
			$config["num_links"] = round($choice);
			$config['first_link'] = 'First';
			$config['next_link'] = 'Next';
			$config['prev_link'] = 'Pre';
			$config['last_link'] = 'Last';

			$this->pagination->initialize($config); //function to show all pages
			$page = ($this->uri->segment($config['uri_segment']) && $this->uri->segment($config['uri_segment']) > 0) ? $this->uri->segment($config['uri_segment']) : 0;
			$data['leave_record'] = ''; // initalize record seleted
			if ($this->uri->segment(3) === 'own_pending' OR !$this->uri->segment(3)) {
				/* get leave recrod as pending */
				$data['leave_record'] = $this->mod_record->approved_padding($config['per_page'], $page, 'own_pending', $this->session->userdata('staff_id'));
			} else if ($this->uri->segment(3) === 'own_approved') {
				/* get leave record as approved */
				$data['leave_record'] = $this->mod_record->approved_padding($config['per_page'], $page, 'own_approved', $this->session->userdata('staff_id'));
			}
			$data['pagination'] = $this->pagination->create_links();
			$this->view_main($data);
		} else {
			redirect('main/');
		}
	}

	/* Update before having approve from validator */

	public function update_before_approve() {
		if ($this->check_session()) {
			$this->session->set_userdata('old_lea_id', $this->uri->segment(3));
			$this->session->set_userdata('old_desc_id', $this->uri->segment(5));
			$this->session->set_userdata('rcd_id', $this->uri->segment(4));
			if ($this->uri->segment(3) == 1) {
				$data['edit_annual'] = PNC_Model::show_detail($this->uri->segment(4), 'annual');
//				var_dump($data['edit_annual']); die;
			} else if ($this->uri->segment(3) == 2) {
				$data['edit_catchup'] = PNC_Model::show_detail($this->uri->segment(4), 'catchup');
			}
			$data['title'] = 'Edit Leaving Request';
			$data['hour'] = array('' => '-- Select --', '4' => '4 Hours', '8' => '8 Hours', '12' => '12 Hours', '16' => '16 Hours');
			$data['rcd_status'] = array('' => '-- Select --', '0' => 'Disapprove', '1' => 'Approve');
			$data['leave_type'] = $this->mod_option->get_leaving_type();
			$data['sub_type'] = $this->mod_option->get_sub_leaving();
			$data['validator'] = $this->mod_option->get_validator();
			$data['ssl_type'] = $this->mod_option->get_ssl_type();
			if ($this->input->post('btn_update')) {
				$old_lea_id = $this->input->post('old_lea_id');
				$old_desc_id = $this->input->post('old_desc_id');
				$record_id = $this->input->post('record_id');
				/* Old post from form update */
				$old_sub_type = $this->input->post('old_sub_type');
				$old_ss_type = $this->input->post('old_ss_type');
				$old_reason = $this->input->post('old_reason');
				$old_start_date = $this->input->post('old_s_date');
				$old_end_date = $this->input->post('old_e_date');
				$old_desc_up = $this->input->post('old_desc_up');
				$old_working_on = $this->input->post('old_working_on');
				$old_catchup_on = $this->input->post('old_catchup_on');
				$old_hour = $this->input->post('old_duration');
				/* Check and Compare value */
				$supper_type = !$this->input->post('super_type') ? $old_lea_id : $this->input->post('super_type');
				$sub_type = !$this->input->post('sub_type') ? $old_sub_type : $this->input->post('sub_type');
				$ss_type = !$this->input->post('ss_type') ? $old_ss_type : $this->input->post('ss_type');
				$reason = !$this->input->post('txt_reason') ? $old_reason : $this->input->post('txt_reason');
				$start_date = !$this->input->post('s_date') ? $old_start_date : $this->input->post('s_date');
				$end_date = !$this->input->post('e_date') ? $old_end_date : $this->input->post('e_date');
				$desc_up = !$this->input->post('desc_up') ? $old_desc_up : $this->input->post('desc_up');
				$working_on = !$this->input->post('working_on') ? $old_working_on : $this->input->post('working_on');
				$catchup_on = !$this->input->post('catchup_on') ? $old_catchup_on : $this->input->post('catchup_on');
				$hour = !$this->input->post('duration') ? $old_hour : $this->input->post('duration');
				$days = ($hour / 8);
				$result = '';
				/* If update with change leave type */
				if ($supper_type != $old_lea_id) {
					if ($supper_type == 1) {
//						echo 'This is sup_type with annaul (Different) '.$sup_type; die();
						PNC_Model::delete_datas('desc_id', $old_desc_id, '', 'pnc_cup_des');
						$result = PNC_Model::update_annual_before($record_id, $supper_type, $sub_type, $ss_type, $reason, $start_date, $end_date, $hour, $days);
					} else if ($supper_type == 2) {
//						echo 'This is sup_type with catch up(Different) '.$sup_type; die();
						$result = PNC_Model::update_catchup_before($record_id, $supper_type, $reason, $start_date, $end_date, $hour, $days, $desc_up, $working_on, $catchup_on);
					}
				} else {
					if ($supper_type == 1) {
//						echo 'This is sup_type with annaul (The same) '.$sup_type; die();
						$result = PNC_Model::update_annual_before($record_id, $supper_type, $sub_type, $ss_type, $reason, $start_date, $end_date, $hour, $days);
					} else if ($supper_type == 2) {
//						echo 'This is sup_type with catch up (The same) '.$sup_type; die();
						$result = PNC_Model::update_catchup($record_id, $old_desc_id, $supper_type, $reason, $start_date, $end_date, $hour, $days, $desc_up, $working_on, $catchup_on);
					}
				}
				if ($result > 0) {
					$this->session->set_userdata('edit_success', show_message('Edited successfully!', 'success'));
					if ($this->session->userdata('per_page')) {
						redirect('info/show_info/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
					}
					redirect('info/show_info/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
				} else {
					$this->session->set_userdata('edit_erro', show_message('Editing was fail!', 'error'));
					redirect('info/update_before_approve/' . '/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
				}
			} else {
				$this->view_main($data);
			}
		} else {
			redirect('main/');
		}
	}

	/* Delete before approved from manager or director */

	public function delete_before_approve() {
		if ($this->check_session()) {
			$this->delete_before_approving('info', 'show_info');
		} else {
			redirect('main/');
		}
	}

//	public function approval() {
//		if ($this->check_session()) {
//			$this->approval_record($this->uri->segment(5), $this->uri->segment(4), $this->uri->segment(3), 'manager', 'info', 'report', 'own_pending', 'type_pending', 'disapprove');
//		} else {
//			redirect('main/');
//		}
//	}
}

/* End of file info.php */
/* Location: ./application/controllers/info.php */